home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cocktail / rex.lha / rex / m2c / Eval.c < prev    next >
C/C++ Source or Header  |  1992-08-18  |  10KB  |  271 lines

  1. #include "SYSTEM_.h"
  2.  
  3. #ifndef DEFINITION_Tree0
  4. #include "Tree0.h"
  5. #endif
  6.  
  7. #ifndef DEFINITION_Strings
  8. #include "Strings.h"
  9. #endif
  10.  
  11. #ifndef DEFINITION_StringMem
  12. #include "StringMem.h"
  13. #endif
  14.  
  15. #ifndef DEFINITION_Sets
  16. #include "Sets.h"
  17. #endif
  18.  
  19. #ifndef DEFINITION_Classes
  20. #include "Classes.h"
  21. #endif
  22.  
  23. #ifndef DEFINITION_Dfa
  24. #include "Dfa.h"
  25. #endif
  26.  
  27. #ifndef DEFINITION_StringMem
  28. #include "StringMem.h"
  29. #endif
  30.  
  31. #ifndef DEFINITION_Tree
  32. #include "Tree.h"
  33. #endif
  34.  
  35. #ifndef DEFINITION_Eval
  36. #include "Eval.h"
  37. #endif
  38.  
  39.  
  40. static Strings_tString String1, String2;
  41. static Sets_tSet Set1, Set2;
  42. static CARDINAL i, j;
  43. static BOOLEAN yyb;
  44. static void yyVisit1 ARGS((Tree0_tTree0 yyt));
  45.  
  46.  
  47. void Eval_Eval
  48. # ifdef __STDC__
  49. (Tree0_tTree0 yyt)
  50. # else
  51. (yyt)
  52. Tree0_tTree0 yyt;
  53. # endif
  54. {
  55.   yyVisit1(yyt);
  56. }
  57.  
  58. static void yyVisit1
  59. # ifdef __STDC__
  60. (Tree0_tTree0 yyt)
  61. # else
  62. (yyt)
  63. Tree0_tTree0 yyt;
  64. # endif
  65. {
  66.   switch (yyt->U_1.V_1.Kind) {
  67.   case Tree0_Node:;
  68.     yyt->U_1.V_3.Node.NodeCount = 0;
  69.     yyt->U_1.V_3.Node.LeafCount = 0;
  70.     yyt->U_1.V_3.Node.IsConstantRE = TRUE;
  71.     yyt->U_1.V_3.Node.Tree = Tree_NoTree;
  72.     break;
  73.   case Tree0_Node1:;
  74.     yyVisit1(yyt->U_1.V_4.Node1.Son1);
  75.     yyt->U_1.V_4.Node1.NodeCount = yyt->U_1.V_4.Node1.Son1->U_1.V_3.Node.NodeCount;
  76.     yyt->U_1.V_4.Node1.LeafCount = yyt->U_1.V_4.Node1.Son1->U_1.V_3.Node.LeafCount;
  77.     yyt->U_1.V_4.Node1.IsConstantRE = TRUE;
  78.     yyt->U_1.V_4.Node1.Tree = Tree_NoTree;
  79.     break;
  80.   case Tree0_Option:;
  81.     yyVisit1(yyt->U_1.V_5.Option.Son1);
  82.     yyt->U_1.V_5.Option.NodeCount = yyt->U_1.V_5.Option.Son1->U_1.V_3.Node.NodeCount;
  83.     yyt->U_1.V_5.Option.LeafCount = yyt->U_1.V_5.Option.Son1->U_1.V_3.Node.LeafCount;
  84.     yyt->U_1.V_5.Option.IsConstantRE = FALSE;
  85.     yyt->U_1.V_5.Option.Tree = Tree_MakeTree1(Tree_nOption, yyt->U_1.V_5.Option.Son1->U_1.V_3.Node.Tree);
  86.     break;
  87.   case Tree0_Repetition:;
  88.     yyVisit1(yyt->U_1.V_6.Repetition.Son1);
  89.     yyt->U_1.V_6.Repetition.NodeCount = yyt->U_1.V_6.Repetition.Son1->U_1.V_3.Node.NodeCount;
  90.     yyt->U_1.V_6.Repetition.LeafCount = yyt->U_1.V_6.Repetition.Son1->U_1.V_3.Node.LeafCount;
  91.     yyt->U_1.V_6.Repetition.IsConstantRE = FALSE;
  92.     yyt->U_1.V_6.Repetition.Tree = Tree_MakeTree1(Tree_nRepetition, yyt->U_1.V_6.Repetition.Son1->U_1.V_3.Node.Tree);
  93.     break;
  94.   case Tree0_Node2:;
  95.     yyVisit1(yyt->U_1.V_7.Node2.Son2);
  96.     yyVisit1(yyt->U_1.V_7.Node2.Son1);
  97.     yyt->U_1.V_7.Node2.NodeCount = yyt->U_1.V_7.Node2.Son1->U_1.V_3.Node.NodeCount + yyt->U_1.V_7.Node2.Son2->U_1.V_3.Node.NodeCount;
  98.     yyt->U_1.V_7.Node2.LeafCount = yyt->U_1.V_7.Node2.Son1->U_1.V_3.Node.LeafCount + yyt->U_1.V_7.Node2.Son2->U_1.V_3.Node.LeafCount;
  99.     yyt->U_1.V_7.Node2.IsConstantRE = TRUE;
  100.     yyt->U_1.V_7.Node2.Tree = Tree_NoTree;
  101.     break;
  102.   case Tree0_List:;
  103.     yyVisit1(yyt->U_1.V_8.List.Son2);
  104.     yyVisit1(yyt->U_1.V_8.List.Son1);
  105.     yyt->U_1.V_8.List.NodeCount = yyt->U_1.V_8.List.Son1->U_1.V_3.Node.NodeCount + yyt->U_1.V_8.List.Son2->U_1.V_3.Node.NodeCount;
  106.     yyt->U_1.V_8.List.LeafCount = yyt->U_1.V_8.List.Son1->U_1.V_3.Node.LeafCount + yyt->U_1.V_8.List.Son2->U_1.V_3.Node.LeafCount;
  107.     yyt->U_1.V_8.List.IsConstantRE = TRUE;
  108.     yyt->U_1.V_8.List.Tree = Tree_MakeTree2(Tree_nList, yyt->U_1.V_8.List.Son1->U_1.V_3.Node.Tree, yyt->U_1.V_8.List.Son2->U_1.V_3.Node.Tree);
  109.     break;
  110.   case Tree0_Sequence:;
  111.     yyVisit1(yyt->U_1.V_9.Sequence.Son2);
  112.     yyVisit1(yyt->U_1.V_9.Sequence.Son1);
  113.     yyt->U_1.V_9.Sequence.NodeCount = yyt->U_1.V_9.Sequence.Son1->U_1.V_3.Node.NodeCount + yyt->U_1.V_9.Sequence.Son2->U_1.V_3.Node.NodeCount;
  114.     yyt->U_1.V_9.Sequence.LeafCount = yyt->U_1.V_9.Sequence.Son1->U_1.V_3.Node.LeafCount + yyt->U_1.V_9.Sequence.Son2->U_1.V_3.Node.LeafCount;
  115.     yyt->U_1.V_9.Sequence.IsConstantRE = yyt->U_1.V_9.Sequence.Son1->U_1.V_3.Node.IsConstantRE && yyt->U_1.V_9.Sequence.Son2->U_1.V_3.Node.IsConstantRE;
  116.     yyt->U_1.V_9.Sequence.Tree = Tree_MakeTree2(Tree_nSequence, yyt->U_1.V_9.Sequence.Son1->U_1.V_3.Node.Tree, yyt->U_1.V_9.Sequence.Son2->U_1.V_3.Node.Tree);
  117.     break;
  118.   case Tree0_Alternative:;
  119.     yyVisit1(yyt->U_1.V_10.Alternative.Son2);
  120.     yyVisit1(yyt->U_1.V_10.Alternative.Son1);
  121.     yyt->U_1.V_10.Alternative.NodeCount = yyt->U_1.V_10.Alternative.Son1->U_1.V_3.Node.NodeCount + yyt->U_1.V_10.Alternative.Son2->U_1.V_3.Node.NodeCount;
  122.     yyt->U_1.V_10.Alternative.LeafCount = yyt->U_1.V_10.Alternative.Son1->U_1.V_3.Node.LeafCount + yyt->U_1.V_10.Alternative.Son2->U_1.V_3.Node.LeafCount;
  123.     yyt->U_1.V_10.Alternative.IsConstantRE = FALSE;
  124.     yyt->U_1.V_10.Alternative.Tree = Tree_MakeTree2(Tree_nAlternative, yyt->U_1.V_10.Alternative.Son1->U_1.V_3.Node.Tree, yyt->U_1.V_10.Alternative.Son2->U_1.V_3.Node.Tree);
  125.     break;
  126.   case Tree0_Ch:;
  127.     yyt->U_1.V_11.Ch.NodeCount = 1;
  128.     yyt->U_1.V_11.Ch.LeafCount = 1;
  129.     yyt->U_1.V_11.Ch.IsConstantRE = TRUE;
  130.     yyt->U_1.V_11.Ch.Tree = Tree_MakeTreeCh(Tree_nChar, Classes_ToClass.A[yyt->U_1.V_11.Ch.Ch]);
  131.     break;
  132.   case Tree0_Set:;
  133.     j = Classes_IsInSetMem(yyt->U_1.V_12.Set.Set);
  134.     if (j != 0) {
  135.       yyt->U_1.V_12.Set.card = Sets_Card(&Classes_SetMemPtr->A[j].Classes);
  136.       Sets_Assign(&Set1, yyt->U_1.V_12.Set.Set);
  137.       Sets_Intersection(&Set1, Classes_CharSet);
  138.       INC1(yyt->U_1.V_12.Set.card, Sets_Card(&Set1));
  139.       if (yyt->U_1.V_12.Set.card == 1) {
  140.         if (Sets_Card(&Classes_SetMemPtr->A[j].Classes) == 1) {
  141.           yyt->U_1.V_12.Set.Tree = Tree_MakeTreeCh(Tree_nChar, CHR(Sets_Select(&Classes_SetMemPtr->A[j].Classes)));
  142.         } else {
  143.           yyt->U_1.V_12.Set.Tree = Tree_MakeTreeCh(Tree_nChar, Classes_ToClass.A[CHR(Sets_Select(&Set1))]);
  144.         }
  145.       } else {
  146.         Sets_MakeSet(&Set2, ORD(Dfa_LastCh));
  147.         if (!Sets_IsEmpty(Set1)) {
  148.           {
  149.             LONGCARD B_1 = Sets_Minimum(&Set1), B_2 = Sets_Maximum(&Set1);
  150.  
  151.             if (B_1 <= B_2)
  152.               for (i = B_1;; i += 1) {
  153.                 if (Sets_IsElement(i, &Set1)) {
  154.                   Sets_Include(&Set2, ORD(Classes_ToClass.A[CHR(i)]));
  155.                 }
  156.                 if (i >= B_2) break;
  157.               }
  158.           }
  159.         }
  160.         if (!Sets_IsEmpty(Classes_SetMemPtr->A[j].Classes)) {
  161.           {
  162.             LONGCARD B_3 = Sets_Minimum(&Classes_SetMemPtr->A[j].Classes), B_4 = Sets_Maximum(&Classes_SetMemPtr->A[j].Classes);
  163.  
  164.             if (B_3 <= B_4)
  165.               for (i = B_3;; i += 1) {
  166.                 if (Sets_IsElement(i, &Classes_SetMemPtr->A[j].Classes)) {
  167.                   Sets_Include(&Set2, i);
  168.                 }
  169.                 if (i >= B_4) break;
  170.               }
  171.           }
  172.         }
  173.         yyt->U_1.V_12.Set.Tree = Tree_MakeTreeSet(Tree_nSet, Set2);
  174.       }
  175.     } else {
  176.       yyt->U_1.V_12.Set.card = Sets_Card(&yyt->U_1.V_12.Set.Set);
  177.       if (yyt->U_1.V_12.Set.card == 1) {
  178.         yyt->U_1.V_12.Set.Tree = Tree_MakeTreeCh(Tree_nChar, CHR(Sets_Select(&yyt->U_1.V_12.Set.Set)));
  179.       } else {
  180.         yyt->U_1.V_12.Set.Tree = Tree_MakeTreeSet(Tree_nSet, yyt->U_1.V_12.Set.Set);
  181.       }
  182.     }
  183.     yyt->U_1.V_12.Set.NodeCount = 1;
  184.     yyt->U_1.V_12.Set.LeafCount = 1;
  185.     yyt->U_1.V_12.Set.IsConstantRE = yyt->U_1.V_12.Set.card == 1;
  186.     break;
  187.   case Tree0_String:;
  188.     yyt->U_1.V_13.String.NodeCount = StringMem_Length(yyt->U_1.V_13.String.String);
  189.     yyt->U_1.V_13.String.LeafCount = StringMem_Length(yyt->U_1.V_13.String.String);
  190.     yyt->U_1.V_13.String.IsConstantRE = TRUE;
  191.     StringMem_GetString(yyt->U_1.V_13.String.String, &String1);
  192.     Strings_AssignEmpty(&String2);
  193.     {
  194.       LONGCARD B_5 = 1, B_6 = Strings_Length(&String1);
  195.  
  196.       if (B_5 <= B_6)
  197.         for (i = B_5;; i += 1) {
  198.           Strings_Append(&String2, Classes_ToClass.A[Strings_Char(&String1, (Strings_tStringIndex)i)]);
  199.           if (i >= B_6) break;
  200.         }
  201.     }
  202.     yyt->U_1.V_13.String.Tree = Tree_MakeTreeString(Tree_nString, StringMem_PutString(&String2));
  203.     break;
  204.   case Tree0_Rule:;
  205.     yyVisit1(yyt->U_1.V_14.Rule.Patterns);
  206.     yyt->U_1.V_14.Rule.NodeCount = yyt->U_1.V_14.Rule.Patterns->U_1.V_3.Node.NodeCount;
  207.     yyt->U_1.V_14.Rule.LeafCount = yyt->U_1.V_14.Rule.Patterns->U_1.V_3.Node.LeafCount;
  208.     yyt->U_1.V_14.Rule.IsConstantRE = TRUE;
  209.     yyt->U_1.V_14.Rule.Tree = Tree_MakeTreeRule(Tree_nRule, yyt->U_1.V_14.Rule.Patterns->U_1.V_3.Node.Tree, yyt->U_1.V_14.Rule.TargetCode, yyt->U_1.V_14.Rule.Line, yyt->U_1.V_14.Rule.CodeMode, yyt->U_1.V_14.Rule.RuleNr);
  210.     break;
  211.   case Tree0_Pattern:;
  212.     yyVisit1(yyt->U_1.V_15.Pattern.RightContext);
  213.     yyVisit1(yyt->U_1.V_15.Pattern.RegExpr);
  214.     if (yyt->U_1.V_15.Pattern.RegExpr->U_1.V_3.Node.IsConstantRE && yyt->U_1.V_15.Pattern.RightContext->U_1.V_3.Node.IsConstantRE) {
  215.       yyt->U_1.V_15.Pattern.card = Sets_Card(&yyt->U_1.V_15.Pattern.StartStates);
  216.       yyt->U_1.V_15.Pattern.NodeCount = (yyt->U_1.V_15.Pattern.RegExpr->U_1.V_3.Node.NodeCount + yyt->U_1.V_15.Pattern.RightContext->U_1.V_3.Node.NodeCount) * yyt->U_1.V_15.Pattern.card;
  217.     } else {
  218.       yyt->U_1.V_15.Pattern.NodeCount = 0;
  219.     }
  220.     if (!(yyt->U_1.V_15.Pattern.RegExpr->U_1.V_3.Node.IsConstantRE && yyt->U_1.V_15.Pattern.RightContext->U_1.V_3.Node.IsConstantRE)) {
  221.       yyt->U_1.V_15.Pattern.LeafCount = yyt->U_1.V_15.Pattern.RegExpr->U_1.V_3.Node.LeafCount + yyt->U_1.V_15.Pattern.RightContext->U_1.V_3.Node.LeafCount;
  222.     } else {
  223.       yyt->U_1.V_15.Pattern.LeafCount = 0;
  224.     }
  225.     yyt->U_1.V_15.Pattern.IsConstantRE = TRUE;
  226.     yyt->U_1.V_15.Pattern.Tree = Tree_MakeTreePattern(Tree_nPattern, yyt->U_1.V_15.Pattern.StartStates, yyt->U_1.V_15.Pattern.RegExpr->U_1.V_3.Node.Tree, yyt->U_1.V_15.Pattern.RightContext->U_1.V_3.Node.Tree, (BOOLEAN)(yyt->U_1.V_15.Pattern.RegExpr->U_1.V_3.Node.IsConstantRE && yyt->U_1.V_15.Pattern.RightContext->U_1.V_3.Node.IsConstantRE), yyt->U_1.V_15.Pattern.PatternNr, yyt->U_1.V_15.Pattern.Position);
  227.     break;
  228.   default :
  229.     break;
  230.   }
  231. }
  232.  
  233. void Eval_BeginEval
  234. # ifdef __STDC__
  235. ()
  236. # else
  237. ()
  238. # endif
  239. {
  240.   Sets_MakeSet(&Set1, ORD(Dfa_LastCh));
  241. }
  242.  
  243. void Eval_CloseEval
  244. # ifdef __STDC__
  245. ()
  246. # else
  247. ()
  248. # endif
  249. {
  250. }
  251.  
  252. void BEGIN_Eval()
  253. {
  254.   static BOOLEAN has_been_called = FALSE;
  255.  
  256.   if (!has_been_called) {
  257.     has_been_called = TRUE;
  258.  
  259.     BEGIN_Tree0();
  260.     BEGIN_Tree0();
  261.     BEGIN_Strings();
  262.     BEGIN_StringMem();
  263.     BEGIN_Sets();
  264.     BEGIN_Classes();
  265.     BEGIN_Dfa();
  266.     BEGIN_StringMem();
  267.     BEGIN_Tree();
  268.  
  269.   }
  270. }
  271.